package org.mcjug.locationmailer.model.template;

import java.net.URL;
import java.util.List;

import org.mcjug.locationmailer.R;

//import android.util.Log;

import com.google.code.rome.android.repackaged.com.sun.syndication.feed.synd.SyndEntry;
import com.google.code.rome.android.repackaged.com.sun.syndication.feed.synd.SyndFeed;
import com.google.code.rome.android.repackaged.com.sun.syndication.io.SyndFeedInput;
import com.google.code.rome.android.repackaged.com.sun.syndication.io.XmlReader;

public class QuoteOfDayTemplateItem extends AbstractTemplateItem implements TemplateItem {

    private static final String TAG = QuoteOfDayTemplateItem.class.getSimpleName();

    @Override
    public int getTemplateItemId() {
        return R.string.emailKeywordQuoteOfDay;
    }

    @Override
    public String getTemplateItemValue() {
        // TODO: replace with preference
        String feedUrl = "http://feeds.feedburner.com/brainyquote/QUOTEFU";
        try {
            URL feedSource = new URL(feedUrl);
            SyndFeedInput input = new SyndFeedInput();
            SyndFeed feed = input.build(new XmlReader(feedSource));
            List<SyndEntry> entries = feed.getEntries();
            if (entries != null && entries.size() > 0) {
                SyndEntry entry = entries.get(0);
                return entry.getDescription().getValue();
            }

        } catch (Throwable e) {
            //Log.e(TAG, "Throwable for feedUrl:" + feedUrl, e);
        }

        return "";
    }

    @Override
    public int getTemplateFlag() {
        return TemplateItem.FLAG_ALL;
    }

}
